package com.vasthu.core.dao;

import com.vasthu.api.dao.RoleDAO;
import com.vasthu.api.domain.Role;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;
import org.apache.log4j.Logger;

public class RoleDAOImpl implements RoleDAO {

    static Logger logger = Logger.getLogger(RoleDAOImpl.class);

    public boolean insertRole(Role role) {
        if (role == null) {
            return false;
        }

        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("INSERT INTO Role(role_name, role_desc, status) VALUES(?, ?, 1)");

            pst.setString(1, role.getRoleName());
            pst.setString(2, role.getRoleDescription());

            pst.executeUpdate();

        } catch (Exception ex) {
            System.out.println("ERROR : RoleDAOImpl->insertRole()");
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    public boolean updateRole(Role role) {
        if (role == null) {
            return false;
        }
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("UPDATE Role SET role_name = " + role.getRoleName()
                    + ", role_desc = " + role.getRoleDescription() + ", status = 1 WHERE (role_id = "
                    + role.getRoleId() + ")");

            pst.executeUpdate();

        } catch (Exception ex) {
            System.out.println("ERROR : UserDAOImpl->insertUser()");
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    public Role getRoleById(long roleId) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Role role = new Role();
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Role WHERE role_id = ? AND status = 1");
            pst.setLong(1, roleId);
            rs = pst.executeQuery();

            while (rs.next()) {
                role.setRoleId(rs.getInt("role_id"));
                role.setRoleName(rs.getString("role_name"));
                role.setRoleDescription(rs.getString("role_desc"));
                break;
            }

        } catch (Exception ex) {
            System.out.println("ERROR : RoleDAOImpl->deleteRoleById()");
        } finally {
            VstDBManager.get().close();
        }
        return role;
    }

    public Role getRoleByName(String roleName) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Role role = new Role();
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Role WHERE role_name = ? AND status = 1");
            pst.setString(1, roleName);
            rs = pst.executeQuery();

            while (rs.next()) {
                role.setRoleId(rs.getInt("role_id"));
                role.setRoleName(rs.getString("role_name"));
                role.setRoleDescription(rs.getString("role_desc"));
                break;
            }

        } catch (Exception ex) {
            System.out.println("ERROR : RoleDAOImpl->deleteRoleByName()");
        } finally {
            VstDBManager.get().close();
        }
        return role;
    }

    public boolean deleteRoleById(long roleId) {
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("DELETE FROM Role WHERE role_id = ?");
            pst.setLong(1, roleId);
            pst.executeUpdate();
        } catch (Exception ex) {
            System.out.println("ERROR : RoleDAOImpl->deleteRoleById()");
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    public List<Role> getAllRoles() {
        List<Role> roleList = new ArrayList<Role>();
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Role WHERE status = 1");
            rs = pst.executeQuery();

            while (rs.next()) {
                Role role = new Role();

                role.setRoleId(rs.getInt("role_id"));
                role.setRoleName(rs.getString("role_name"));
                role.setRoleDescription(rs.getString("role_desc"));

                roleList.add(role);
            }

        } catch (SQLException ex) {
            System.out.println("ERROR : UserDAOImpl->getAllUsers()");
        } finally {
            VstDBManager.get().close();
        }
        return roleList;
    }
}